﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;
using System.Xml;

namespace MixScriptWeb.Accounting
{
    public partial class fbeginingSubdiary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData(3);
                makeItEnable(false);
                gvSub.DataSource = null;
                gvSub.DataBind();
            }
        }
        private void makeItEnable(bool enable)
        {
            btnSave.Enabled = enable;
            btnReset.Enabled = enable;
        }
        private void LoadData(int type)
        {
            try
            {
                cAccSubLedger oSubL = new cAccSubLedger();
                DataTable dt = new DataTable();

                if (oSubL.getSubsidiaryData(ref dt, Convert.ToString(type)))
                { }
                else { }
                gvmaster.DataSource = dt;
                gvmaster.DataBind();
                if (dt.Rows.Count > 0)
                    makeItEnable(true);
                else
                    makeItEnable(false);
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText(Request.MapPath(Request.ApplicationPath) + "Error.log", ex.Message + Environment.NewLine);

            }

        }
        private void LoadSubData(string accid, string dbcd, string curr)
        {
            int type = 3;
            if (rbBank.Checked)
                type = 3;
            else if (rbCustomer.Checked)
                type = 4;
            else if (rbEmployee.Checked)
                type = 6;
            else if (rbSupplier.Checked)
                type = 7;

            cAccSubLedger oSubL = new cAccSubLedger();
            DataTable dt = new DataTable();
            oSubL.getSubSubsidiary(ref dt, type, accid, dbcd, curr);
            gvSub.DataSource = dt;
            gvSub.DataBind();
            if (dt.Rows.Count > 0)
                makeItEnable(true);
            else
                makeItEnable(false);


        }
        protected void gvMaster_SelectedChanges(object sender, EventArgs e)
        {
            lblSubName.Text = "";
            string idcoa;
            idcoa = gvmaster.SelectedRow.Cells[1].Text;
            ViewState["idcoa"] = idcoa;
            lblSubName.Text = gvmaster.SelectedRow.Cells[2].Text;
            LoadSubData(idcoa, gvmaster.SelectedRow.Cells[3].Text, gvmaster.SelectedRow.Cells[4].Text);
        }
        private void clearTemp(int type)
        {
            if (type == 1)
            {
                gvSub.Columns[2].HeaderStyle.CssClass = "";
                gvSub.Columns[3].HeaderStyle.CssClass = "";
                gvSub.Columns[2].ItemStyle.CssClass = "";
                gvSub.Columns[3].ItemStyle.CssClass = "";
            }
            else
            {


            }
            gvSub.DataSource = null;
            gvSub.DataBind();
            makeItEnable(false);
        }

        protected void rbBank_CheckedChanges(Object sender, EventArgs e)
        {
            LoadData(3);
            clearTemp(1);
            gvSub.DataSource = null;
            gvSub.DataBind();
            makeItEnable(false);
        }

        protected void rbCustomer_CheckedChanges(Object sender, EventArgs e)
        {
            LoadData(4);
            clearTemp(0);
            gvSub.DataSource = null;
            gvSub.DataBind();
            makeItEnable(false);
        }

        protected void rbEmployee_CheckedChanges(Object sender, EventArgs e)
        {
            LoadData(6);
            clearTemp(0);
            gvSub.DataSource = null;
            gvSub.DataBind();
            makeItEnable(false);
        }

        protected void rbSupplier_CheckedChanges(Object sender, EventArgs e)
        {
            LoadData(7);
            clearTemp(0);
            gvSub.DataSource = null;
            gvSub.DataBind();
            makeItEnable(false);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            cAccSubLedger oSubL;
            Int32 type = 3;
            if (rbBank.Checked)
                type = 3;
            else if (rbCustomer.Checked)
                type = 4;
            else if (rbEmployee.Checked)
                type = 6;
            else if (rbSupplier.Checked)
                type = 7;
            TextBox txt = new TextBox();
            for (int i = 0; i < gvSub.Rows.Count - 1; i++)
            {

                if (gvSub.Rows[i].Cells[2].Text == "D")
                    txt = (TextBox)gvSub.Rows[i].FindControl("txtDebit");
                else
                    txt = (TextBox)gvSub.Rows[i].FindControl("txtCredit");

                oSubL = new cAccSubLedger();
                oSubL.SaveSubSidiary((string)ViewState["idcoa"], type, gvSub.Rows[i].Cells[0].Text,
                     gvSub.Rows[i].Cells[2].Text.Replace("&nbsp;", ""), txt.Text, 0);

            }
            LoadData(type);
        }
    }
}